package server_commands;

import static server.RLM_Server.activeConnections;

import java.sql.SQLException;
import java.util.StringTokenizer;
import java.util.Vector;

import server.ServerSocketConnection;
import utils.GenerateID;
import connection_modul.DatabaseConnection;

public class DELCommand extends Command {
	
	
	public DELCommand(String parameter) {
		setCode(DEL_CMD);
		setParameter(parameter);
	}
	
	/**
	 * Metoda vrsi brisanje zeljenog korisnika sa liste onog ko zahteva. Ukoliko korisnik, koji se brise, nije
	 * bio blokiran, automatski se dodaje u blok listu i obavestava se o promeni statusa onog
	 * ko ga je obrisao. I
	 */	
	
	public boolean executeCommand(ServerSocketConnection connection, DatabaseConnection dbConnection) {
		
		StringTokenizer tokenizer = new StringTokenizer(getParameter());
		String username = tokenizer.nextToken();
		String deletedUser = tokenizer.nextToken();
		
		// korisnik se brise iz tabele tblContacts
		try {
			dbConnection.doUpdateStatement("DELETE FROM tblContacts " +
						"WHERE tblContacts.user_source ='"+username+"' " +
						"AND tblContacts.user_dest ='"+deletedUser+"'");
			
		
		
			dbConnection.clearDataStore();		
			
			// proverava se da li je vec blokiran
			String SQLQuery = "SELECT * FROM tblBlocks" +
						  " WHERE tblBlocks.user_source = '" + username + "' AND" +
						  " tblBlocks.user_dest = '" + deletedUser + "'";
			
			dbConnection.doStatement(SQLQuery);
				

			// ukoliko je korisnik blokiran
			if( dbConnection.data.size() != 0 ) {
				connection.setMessage("100 OK");
				dbConnection.doCommit();
				return true;
			}
			
			dbConnection.clearDataStore();
			
			SQLQuery = "INSERT INTO tblBlocks (block_id, user_source, user_dest)" +
//					" VALUES ('" + username + "', '" + deletedUser + "')";
					" VALUES (" + GenerateID.generateID() + ", '" + username + "', '" + deletedUser + "')";
			
			dbConnection.doUpdateStatement(SQLQuery);			
			
//			ServerSocketConnection tempConnection = activeConnections.get(deletedUser);
//			
//			if (tempConnection != null) {
//				tempConnection.setMessage("SERV_DEL " + username);
//			}
			
			String selectSQLQuery = "SELECT tblUsers.nickname, tblUsers.email, tblUsers.avatar " +
				"FROM tblUsers " +
				"WHERE tblUsers.username = '" + username + "'";
			
			dbConnection.doStatement(selectSQLQuery);
			
			ServerSocketConnection tempConnection = activeConnections.get(deletedUser);
			
			Vector row = dbConnection.data.get(0);
			
			String nickname = row.get(0).toString();
			String email = row.get(1).toString();
			String avatar = row.get(2).toString();
			
			if (tempConnection != null) {
//				tempConnection.setMessage("SERV_BLOCK " + username);
				tempConnection.setMessage("SERV_CHSTATUS " + username + " Offline " + nickname + " " + email + " " + avatar);
			}
				
				connection.setMessage("100 Deleted OK");
		
		} catch (SQLException e) {		
			e.printStackTrace();
			dbConnection.doRollBack();
			return false;
		}
		
		dbConnection.doCommit();
		
		return true;
	}

}
